<?php
/*
usage:
php xp_console_generate_plugin.php fastadmin_addons_all csmcas
*/
// 获取传入的参数
$projectname = $argv[1];
$pluginname = $argv[2]; 
$uc_pluginname = ucfirst($pluginname);

include "xp_project_function.php";

$dist_plugin = "dist/{$projectname}";

deleteDirectory($dist_plugin);
createDirectory($dist_plugin);

createDirectory("{$dist_plugin}/addons");
createDirectory("{$dist_plugin}/addons/xpframework");

$exceptfilenames = [
    "addons/xpframework/uniapp/xpframework_mobile/node_modules",
];
xcopy("addons/xpframework","{$dist_plugin}/addons/xpframework",$exceptfilenames);
rename("{$dist_plugin}/addons/xpframework/_deploy/env_dev/uniapp/xpframework_mobile","{$dist_plugin}/addons/xpframework/_deploy/env_dev/uniapp/{$pluginname}_mobile");
rename("{$dist_plugin}/addons/xpframework/_deploy/env_plugin/uniapp/xpframework_mobile","{$dist_plugin}/addons/xpframework/_deploy/env_plugin/uniapp/{$pluginname}_mobile");
rename("{$dist_plugin}/addons/xpframework/library/xapp/xpframework","{$dist_plugin}/addons/xpframework/library/xapp/{$pluginname}");
rename("{$dist_plugin}/addons/xpframework/uniapp/xpframework_mobile/pages/xpframework","{$dist_plugin}/addons/xpframework/uniapp/xpframework_mobile/pages/{$pluginname}");
rename("{$dist_plugin}/addons/xpframework/uniapp/xpframework_mobile/static/xpframework","{$dist_plugin}/addons/xpframework/uniapp/xpframework_mobile/static/{$pluginname}");
rename("{$dist_plugin}/addons/xpframework/uniapp/xpframework_mobile","{$dist_plugin}/addons/xpframework/uniapp/{$pluginname}_mobile");
rename("{$dist_plugin}/addons/xpframework/Xpframework.php","{$dist_plugin}/addons/xpframework/{$uc_pluginname}.php");
rename("{$dist_plugin}/addons/xpframework","{$dist_plugin}/addons/{$pluginname}");
echo "> Finish addons/xpframework\r\n\r\n";

$modules = ["admin","api","index","xpframework"];
foreach($modules as $module){
    createDirectory("{$dist_plugin}/application");
    createDirectory("{$dist_plugin}/application/{$module}");
    createDirectory("{$dist_plugin}/application/{$module}/controller");
    createDirectory("{$dist_plugin}/application/{$module}/controller/xpframework");
    xcopy("application/{$module}/controller/xpframework","{$dist_plugin}/application/{$module}/controller/xpframework");
    rename("{$dist_plugin}/application/{$module}/controller/xpframework","{$dist_plugin}/application/{$module}/controller/{$pluginname}");

    createDirectory("{$dist_plugin}/application/{$module}/lang");
    createDirectory("{$dist_plugin}/application/{$module}/lang/zh-cn");
    createDirectory("{$dist_plugin}/application/{$module}/lang/zh-cn/xpframework");
    xcopy("application/{$module}/lang/zh-cn/xpframework","{$dist_plugin}/application/{$module}/lang/zh-cn/xpframework");
    rename("{$dist_plugin}/application/{$module}/lang/zh-cn/xpframework","{$dist_plugin}/application/{$module}/lang/zh-cn/{$pluginname}");

    createDirectory("{$dist_plugin}/application/{$module}/model");
    createDirectory("{$dist_plugin}/application/{$module}/model/xpframework");
    xcopy("application/{$module}/model/xpframework","{$dist_plugin}/application/{$module}/model/xpframework");
    rename("{$dist_plugin}/application/{$module}/model/xpframework","{$dist_plugin}/application/{$module}/model/{$pluginname}");

    createDirectory("{$dist_plugin}/application/{$module}/validate");
    createDirectory("{$dist_plugin}/application/{$module}/validate/xpframework");
    xcopy("application/{$module}/validate/xpframework","{$dist_plugin}/application/{$module}/validate/xpframework");
    rename("{$dist_plugin}/application/{$module}/validate/xpframework","{$dist_plugin}/application/{$module}/validate/{$pluginname}");

    createDirectory("{$dist_plugin}/application/{$module}/view");
    createDirectory("{$dist_plugin}/application/{$module}/view/xpframework");
    xcopy("application/{$module}/view/xpframework","{$dist_plugin}/application/{$module}/view/xpframework");
    rename("{$dist_plugin}/application/{$module}/view/xpframework","{$dist_plugin}/application/{$module}/view/{$pluginname}");
}
rename("{$dist_plugin}/application/xpframework","{$dist_plugin}/application/{$pluginname}");

createDirectory("{$dist_plugin}/public");
createDirectory("{$dist_plugin}/public/assets");
createDirectory("{$dist_plugin}/public/assets/addons");
createDirectory("{$dist_plugin}/public/assets/addons/xpframework");
xcopy("public/assets/addons/xpframework","{$dist_plugin}/public/assets/addons/xpframework");
rename("{$dist_plugin}/public/assets/addons/xpframework","{$dist_plugin}/public/assets/addons/{$pluginname}");

createDirectory("{$dist_plugin}/public/assets/js");
createDirectory("{$dist_plugin}/public/assets/js/backend/xpframework");
createDirectory("{$dist_plugin}/public/assets/js/frontend/xpframework");
xcopy("public/assets/js/backend/xpframework","{$dist_plugin}/public/assets/js/backend/xpframework");
xcopy("public/assets/js/frontend/xpframework","{$dist_plugin}/public/assets/js/frontend/xpframework");
rename("{$dist_plugin}/public/assets/js/backend/xpframework","{$dist_plugin}/public/assets/js/backend/{$pluginname}");
rename("{$dist_plugin}/public/assets/js/frontend/xpframework","{$dist_plugin}/public/assets/js/frontend/{$pluginname}");

copy("xp_function.php","{$dist_plugin}/xp_function.php");
copy("xp_project_console_package_plugin.php","{$dist_plugin}/xp_project_console_package_plugin.php");
copy("xp_project_function.php","{$dist_plugin}/xp_project_function.php");
copy("xp_project_docs.md","{$dist_plugin}/xp_project_docs.md");
copy("xp_project_xpframework_version.md","{$dist_plugin}/xp_project_xpframework_version.md");
copy("xp_project_xpframework_version.md","{$dist_plugin}/addons/{$pluginname}/library/version.md");


//todo replace content xpframework->csmwj Xpframework->Csmwj
replaceInFiles($dist_plugin,"xpframework",$pluginname);
replaceInFiles($dist_plugin,"Xpframework",$uc_pluginname);


